using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;

public class VActInputDefine { }
public class VInputOverride
{
    protected VInputSample ips;
    protected VActInput input;

    public List<VInputUnit> inputUnits;

    public VInputOverride()
    {
        inputUnits = new List<VInputUnit>() { 
            new VInputUnit()
            {
                oriKey = "Space",
                oriAct = "Jump"
            }
        };
    }

    public void InitLoader(VInputSample ips, VActInput input)
    {
        this.ips = ips;
        this.input = input;
    }

    public List<VInputToActMapper> GenMapperList()
    {
        var list = inputUnits.Select(x => GenMapper(x)).Where(x => x != null).ToList();
        return list;
    }


    public virtual void LoadAudioOverride(VActProcessor processor)
    {

    }

    public VInputToActMapper GenMapper(VInputUnit unit)
    {
        if (string.IsNullOrEmpty(unit.oriKey)) return null;

        var mapper = new VInputToActMapper()
        {
            oriKey = FindKey(unit.oriKey),
            oriAct = FindAct(unit.oriAct),
        };

        if(mapper.oriKey==null || mapper.oriAct==null) return null;

        int mCounts = unit.modifyKeys.Count;
        if (mCounts <= 0) return mapper;
        mapper.mKeys = new List<InputAction>();
        mapper.mActs = new List<VAct>();
        for (int i = 0; i < unit.modifyKeys.Count; i++)
        {
            mapper.mKeys.Add(FindKey(unit.modifyKeys[i]));
            mapper.mActs.Add(FindAct(unit.modifyActs[i]));
        }
        return mapper;
    }

    public InputAction FindKey(string name) => ips.FindAction(name);
    public VAct FindAct(string name) => input.vActs.Find(act => act.name == name);
}

public class VInput_DogOverride : VInputOverride
{
    public override void LoadAudioOverride(VActProcessor processor)
    {
        var lifeCore = processor.gameObject.GetComponent<VLifeCore>();
        if (lifeCore == null) throw new System.Exception("没有添加LifeCore");
        var dogcore = (VLifeCore_Dog) lifeCore;
        VAudio.onSpeechRecognition += (speech) =>
        {
            //也就是说这个地方不适合填load
            Debug.Log("语音识别:" + speech);
            dogcore.UserBattle(speech);
        };
    }
}

public class VInput_HuTaoOverride : VInputOverride
{
    public override void LoadAudioOverride(VActProcessor processor)
    {
        var lifeCore = processor.gameObject.GetComponent<VLLMCharacter_Hutao>();

        VAudio.onSpeechRecognition += (speech) =>
        {
            //也就是说这个地方不适合填load
            Debug.Log("语音识别:" + speech);
            lifeCore.CharacterTalk(speech);
        };
    }
}

